package com.test.mywebtest.util;


import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.FileEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

public class HttpClientUtil {
    /*
     * http 协议
     *
     * */
    public static String postMap(String url, Map<String, Object> map) throws IOException {
        CloseableHttpClient httpClient = null;
        HttpPost httpPost = null;
        BufferedReader reader = null;
        String result = null;
        try {
            httpClient = HttpClientBuilder.create().build();
            httpPost = new HttpPost(url);
            //设置参数
            List<NameValuePair> list = new ArrayList<NameValuePair>();
            Iterator iterator = map.entrySet().iterator();
            while (iterator.hasNext()) {
                Map.Entry<String, String> elem = (Map.Entry<String, String>) iterator.next();
                list.add(new BasicNameValuePair(elem.getKey(), String.valueOf(elem.getValue())));
            }
            if (list.size() > 0) {
                UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list, "utf-8");
                httpPost.setEntity(entity);
            }
            HttpResponse response = httpClient.execute(httpPost);
            if (response != null) {
                HttpEntity resEntity = response.getEntity();
                if (resEntity != null) {
                    reader = new BufferedReader(new InputStreamReader(resEntity.getContent(), "UTF-8"));
                    StringBuffer sb = new StringBuffer();
                    String line = null;
                    while ((line = reader.readLine()) != null) {
                        if (line.contains(":")) {
                            sb.append(line);
                            sb.append(",");
                        }
                    }
                    result = sb.toString();
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            if (httpPost != null) {
                httpPost.abort();
            }
            if (reader != null) {
                reader.close();
            }
            if (httpClient != null) {
                //httpclient.getConnectionManager().shutdown();
            }
        }
        return result;
    }


    /**
     * HttpClientUtil post请求工具类
     * File
     *
     * @see [类、类#方法、类#成员]
     */
    public static String postFile(String url, String filePath) {
        CloseableHttpClient httpClient = null;
        HttpPost httpPost = null;
        BufferedReader reader = null;

        try {
            httpClient = HttpClientBuilder.create().build();
            httpPost = new HttpPost(url);
            FileEntity bin = new FileEntity(new File(filePath), "application/zip");
            httpPost.setEntity(bin);
            HttpResponse response = httpClient.execute(httpPost);
            HttpEntity resEntity = response.getEntity();
            if (resEntity != null) {
                reader = new BufferedReader(new InputStreamReader(resEntity
                        .getContent(), "UTF-8"));
                StringBuffer sb = new StringBuffer();
                String line = null;
                while ((line = reader.readLine()) != null) {
                    sb.append(line);
                    sb.append("\r\n");
                }
                return sb.toString();
            }

        } catch (Exception e) {
            System.out.println("not connect:" + url + "\n" + e.getMessage());
        } finally {
            if (httpPost != null) {
                httpPost.abort();
            }
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (httpClient != null) {
                //httpclient.getConnectionManager().shutdown();
            }
        }

        return "none";
    }

    public static void main(String[] args) {
        String url = "http://192.168.65.171:7080/ErmsServer/resource/ermsopeninfaceBJ/getToken";
        Map<String, Object> tokenMap = new HashMap<String, Object>();
        tokenMap.put("username", "qup");
        tokenMap.put("password", "123456");
        tokenMap.put("unit", "公文系统OA");
        try {
            String s = postMap(url, tokenMap);
            System.out.println(s);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
